package com.earthchen.core.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.earthchen.core.util.FileUtil;
import com.earthchen.core.util.WebUtil;
import com.earthchen.core.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @author earthchen
 * @date 2018/9/2
 **/
public class BaseController {


    protected static ResultVO SUCCESS = ResultVO.success();

    protected HttpServletRequest getHttpServletRequest() {
        return WebUtil.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return WebUtil.getResponse();
    }

    protected HttpSession getSession() {
        return WebUtil.getRequest().getSession();
    }

    protected HttpSession getSession(Boolean flag) {
        return WebUtil.getRequest().getSession(flag);
    }

    protected String getParameter(String name) {
        return WebUtil.getRequest().getParameter(name);
    }

    protected Map<String, String> getRequestBodyMap(ServletRequest request) {
        return WebUtil.getRequestBodyMap(request);
    }

    protected void setAttr(String name, Object value) {
        WebUtil.getRequest().setAttribute(name, value);
    }

    protected Integer getSystemInvokCount() {
        return (Integer) this.getHttpServletRequest().getServletContext().getAttribute("systemCount");
    }


    /**
     * 将list包装成page对象
     *
     * @param list
     * @param <T>
     * @return
     */
    protected <T> Page<T> wrapPageResult(List<T> list) {
        Page<T> page = new Page<>();
        Pagination pagination = PageHelper.getPagination();
        page.setRecords(list);
        BeanUtils.copyProperties(pagination, page);
        return page;
    }

//    /**
//     * 把service层的分页信息，封装为bootstrap table通用的分页封装
//     */
//    protected <T> PageInfoBT<T> packForBT(Page<T> page) {
//        return new PageInfoBT<T>(page);
//    }

//    /**
//     * 包装一个list，让list增加额外属性
//     */
//    protected Object warpObject(BaseControllerWarpper warpper) {
//        return warpper.warp();
//    }

    /**
     * 删除cookie
     */
    protected void deleteCookieByName(String cookieName) {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }

    /**
     * 删除所有cookie
     */
    protected void deleteAllCookie() {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for (Cookie cookie : cookies) {
            Cookie temp = new Cookie(cookie.getName(), "");
            temp.setMaxAge(0);
            this.getHttpServletResponse().addCookie(temp);
        }
    }

    /**
     * 返回前台文件流
     *
     * @author fengshuonan
     * @date 2017年2月28日 下午2:53:19
     */
    protected ResponseEntity<byte[]> renderFile(String fileName, String filePath) {
        byte[] bytes = FileUtil.toByteArray(filePath);
        return renderFile(fileName, bytes);
    }

    /**
     * 返回前台文件流
     *
     * @author fengshuonan
     * @date 2017年2月28日 下午2:53:19
     */
    protected ResponseEntity<byte[]> renderFile(String fileName, byte[] fileBytes) {
        String dfileName = null;
        try {
            dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", dfileName);
        return new ResponseEntity<byte[]>(fileBytes, headers, HttpStatus.CREATED);
    }
}
